<?php 

########################################## 
#            BBCode parser 1.0 
# Description:    Convert BBCode style text to html 
# Developed by:   Ramunas Pilotas 
# Email:          woolfas@gmail.com  
# Licence:        GLP 
##########################################  

// Warning: 
//   Although this class should not have unexpected results, the use of this 
//   class is at the risk of the user. The author can not be responsible 
//   if any damage is caused by this class. 
class BBCode { 
  private static $simple_search = array( 
 	'/\[url\](.*?)\[\/url\]/is', 
    '/\[url\=(.*?)\](.*?)\[\/url\]/is', 
     "/\[youtube\](.*?)\[\/youtube\]/i"
  
  ); 
   
  private static $simple_replace = array( 
    '<a href="$1" rel="nofollow">$1</a>', 
    '<a href="$1" rel="nofollow">$2</a>', 
	"<div class=\"video-container\"><iframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe></div>"  
 /*    "<object width=\"425\" height=\"350\"><embed src=\"http://www.youtube.com/v/$1\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object>", */
   
   
   
  ); 

 public static function parse($string) { 
     
    $s = (string) $string; 
     
    if (empty($s)) { 
      return ''; 
    } 
     
    // Preappend http:// to url address if not present 
    $s = preg_replace('/\[url\=([^(http)].+?)\](.*?)\[\/url\]/i', '[url=http://$1]$2[/url]', $s); 
    $s = preg_replace('/\[url\]([^(http)].+?)\[\/url\]/i', '[url=http://$1]$1[/url]', $s); 
    $s=str_replace('<div></div>','<Br/>',$s);
    // Add line breaks 
    //$s = nl2br($s); 
        
    // Parse bbcode 
    $s = preg_replace(self::$simple_search, self::$simple_replace, $s); 
    return $s; 
  } 

} 

/* echo BBCode::parse($bbtext);  */
?>